import { getSubjectsListApi, majorsListApi } from "@/api/table";
export function getSortIndex(key, listData, page) {
    for (let i in listData[page - 1]) {
        if (listData[page - 1][i].key == key) {
            return i;
        }
    }
}
export function Formatter(row, column, cellValue, index) {
    const dateTme = row;
    let date = new Date(dateTme);
    const Y = date.getFullYear();
    const M =
        date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1;
    const D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    const h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    const m =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    const s =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;
}

export function tableDataProcess(data, pagesSize) {
    for (let i in data) {
        data[i].listStatus = true
        // console.log(data)
        if (data[i].status) {
            data[i].status = true;
        } else {
            data[i].status = false;
        }
    }

    //分隔页
    let newListData = []
    for (let i = 0; i < Math.ceil(data.length / pagesSize); i++) {
        newListData.push(data.slice(i * pagesSize, i * pagesSize + pagesSize))
    }
    return { newListData, data }
}


export function getSubjectOption() {
    //获取专业表
    let subjectOption = []
    let majorsList = []
    majorsListApi().then((res) => {
        
        for (let i in res.data.data) {
            if (res.data.data[i].status) {
                res.data.data[i].status = false;
            } else {
                res.data.data[i].status = true;
            }
            subjectOption.push({
                value: res.data.data[i].major_name,
                label: res.data.data[i].major_name,
                disabled: res.data.data[i].status,
                children: []
            })
            let { status, major_name } = res.data.data[i];
            majorsList.push({
                major_name: major_name,
                status: status,
            });
        }
        //获取科目表
        getSubjectsListApi().then((res) => {
            let sujectList = []
            for (let i in res.data.data) {
                if (res.data.data[i].status) {
                    res.data.data[i].status = false;
                } else {
                    res.data.data[i].status = true;
                }
                sujectList.push({
                    major_name: res.data.data[i].major_name,
                    subject_name: res.data.data[i].subject_name,
                    status: res.data.data[i].status,
                });
            }
            for (let j in subjectOption) {
                for (let l in sujectList) {
                    if (sujectList[l].major_name == subjectOption[j].label) {
                        subjectOption[j].children.push({
                            label: sujectList[l].subject_name,
                            value: sujectList[l].subject_name,
                            disabled: sujectList[l].status
                        })
                    }
                }
            }
        });
    });
    return subjectOption
}